﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#region Version Info
/* ========================================================================
* 【本页面功能概述】
*
* 作者：wangjianfeng 创建时间：2012/9/15 16:39:24
* 文件名：Category.cs
*
* 修改者： 时间：
* 修改说明：
* ========================================================================
*/
#endregion
namespace ChinaLeg.Cache
{
    public class Category
    {
        #region 类实例化单件模式
        static Category instance = null;
        static readonly object padlock = new object();

        public static Category Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (padlock)
                    {
                        if (instance == null)
                        {
                            instance = new Category();
                        }
                    }
                }
                return instance;
            }
        }
        #endregion

        /// <summary>
        /// 获取类别列表
        /// </summary>
        /// <returns></returns>
        public List<ChinaLeg.Model.Category> GetList()
        {
            string key = ChinaLeg.Cache.CacheKeys.Keys["Category"];
            if (System.Web.HttpContext.Current.Cache[key] == null)
            {
                List<ChinaLeg.Model.Category> list = ChinaLeg.BLL.Category.Instance.GetList();
                System.Web.HttpContext.Current.Cache.Insert(key, list, null, DateTime.Now.AddDays(1), TimeSpan.Zero);
                return list;
            }
            else
            {
                return System.Web.HttpContext.Current.Cache[key] as List<ChinaLeg.Model.Category>;
            }

        }

        /// <summary>
        /// 获取类别树形菜单
        /// </summary>
        /// <returns></returns>
        public List<ChinaLeg.Model.OptionItem> GetTreeList()
        {
            List<ChinaLeg.Model.Category> list = GetList();

            var items = from p in list where p.ParentId == -1 orderby p.ShowOrder select new { p.Id,p.CatName};
            List<ChinaLeg.Model.OptionItem> newlist = new List<Model.OptionItem>();
            foreach (var item in items)
            {
                newlist.Add(new Model.OptionItem(item.Id.ToString(), item.CatName));
                var nodes = from p in list where p.ParentId == item.Id orderby p.ShowOrder select new { p.Id,p.CatName};
                foreach (var node in nodes)
                {
                    newlist.Add(new Model.OptionItem(node.Id.ToString(),"--"+node.CatName));
                }
            }
            return newlist;
        }

        /// <summary>
        /// 获取一级分类
        /// </summary>
        /// <returns></returns>
        public List<ChinaLeg.Model.OptionItem> GetNotParentCatList()
        {
            List<ChinaLeg.Model.Category> list = GetList();

            var items = list.Where(p => p.ParentId == -1).OrderBy(p=>p.ShowOrder).Select(p => new { p.Id, p.CatName });
            List<ChinaLeg.Model.OptionItem> newlist = new List<Model.OptionItem>();
            foreach (var item in items)
            {
                newlist.Add(new Model.OptionItem(item.Id.ToString(), item.CatName));
            }
            return newlist;
        }

        /// <summary>
        /// 获取一级分类
        /// </summary>
        /// <returns></returns>
        public List<ChinaLeg.Model.OptionItem> GetOneLevelList()
        {
            List<ChinaLeg.Model.Category> list = GetList();

            var items = list.Where(p => p.PageType == 1).OrderBy(p => p.ShowOrder).Select(p => new { p.Id, p.CatName });
            List<ChinaLeg.Model.OptionItem> newlist = new List<Model.OptionItem>();
            foreach (var item in items)
            {
                newlist.Add( new Model.OptionItem( item.Id.ToString(),item.CatName));
            }
            return newlist;
        }
        /// <summary>
        /// 获取二级分类
        /// </summary>
        /// <returns></returns>
        public List<ChinaLeg.Model.Category>  GetChildLevelList(int ParentId)
        {
            List<ChinaLeg.Model.Category> list = GetList();

            var items = list.Where(p => p.ParentId == ParentId).OrderBy(p => p.ShowOrder).Select(p=>p);

            return items.ToList<ChinaLeg.Model.Category>();
        }

        /// <summary>
        /// 获取二级分类的ID 
        /// </summary>
        /// <returns>1,2,3,4</returns>
        public string GetChildLevelIds(int ParentId)
        {
            string str = "";
            List<ChinaLeg.Model.Category> list = GetChildLevelList(ParentId);
            foreach (ChinaLeg.Model.Category model in list)
            {
                str += ","+model.Id;
            }
            if (str != "")
            {
                return str.Substring(1);
            }
            return str;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="catid"></param>
        /// <param name="title"></param>
        /// <param name="keywords"></param>
        /// <param name="desc"></param>
        /// <param name="bgImage"></param>
        public void GetModel(int catid, out string title, out string keywords, out string desc, out string bgImage)
        {
            List<ChinaLeg.Model.Category> list = GetList();
            var items = list.Where(p=>p.Id==catid);
            if (items.Count() > 0)
            {
                foreach (var item in items)
                {
                    title = item.Title;
                    keywords = item.KeyWords;
                    desc = item.Description;
                    bgImage = item.BgImage;
                    if (string.IsNullOrEmpty(bgImage))
                    {
                        var parents = list.Where(p => p.Id == item.ParentId);
                        foreach (var it in parents)
                        {
                            bgImage = it.BgImage;
                        }
                    }
                    return;
                }
            }
            
            title = "";
            keywords = "";
            desc = "";
            bgImage = "";
            
        }

        public int GetParentId(int catid)
        {
            List<ChinaLeg.Model.Category> list = GetList();
            var items = list.Where(p=>p.Id==catid);
            foreach (var item in items)
            {
                return item.ParentId;
            }
            return catid;
        }

        public string GetCatName(int catid)
        {
            List<ChinaLeg.Model.Category> list = GetList();
            var items = list.Where(p => p.Id == catid);
            foreach (var item in items)
            {
                return item.CatName;
            }
            return "";
        }
    }
}
